package hmvictor.jpql;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;

@ActionID(
        category = "Edit",
        id = "hmvictor.jpql.CopyAction"
)
@ActionRegistration(
        displayName = "#CTL_CopyAction"
)
@ActionReference(path = "Menu/Edit", position = 1150)
@Messages("CTL_CopyAction=Copy as Java Code")
public final class CopyAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            JPQLEditorTopComponent editor=(JPQLEditorTopComponent) WindowManager.getDefault().findTopComponent("JQPLEditorTopComponent");
            String selectedText = editor.getSelectedText();
            if(selectedText != null) {
                StringSelection stringSelection = new StringSelection(Queries.getJavaCodeFromQuery(selectedText));
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, stringSelection);
            }
        } catch (IllegalStateException ex) {
            
        }
    }
}
